package com.jackaltsc.mydemodatastorage.contentprovider;

import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;

public class MyContentProvider extends ContentProvider {

    private MySQLiteHelper mySQLiteHelper = null;

    private static final String authority =
            "com.jackaltsc.mydemodatastorage.contentprovider.MyContentProvider";

    //匹配Uri
    private static final UriMatcher URI_MATCHER = new UriMatcher(UriMatcher.NO_MATCH);

    //区分请求操作是针对单条记录还是多条记录
    private static final int STUDENT = 1;
    private static final int STUDENTS = 2;

    static {

        URI_MATCHER.addURI(authority, MySQLiteHelper.TABLEC_NAME, STUDENTS);
        URI_MATCHER.addURI(authority, MySQLiteHelper.TABLEC_NAME + "/#", STUDENT);
    }

    public MyContentProvider() {
    }

    @Override
    public boolean onCreate() {

        mySQLiteHelper = new MySQLiteHelper(getContext());
        return false;
    }

    @Override
    public Cursor query(Uri uri, String[] projection, String selection,
                        String[] selectionArgs, String sortOrder) {

        Cursor cursor = null;

        try {

            int flag = URI_MATCHER.match(uri);

            SQLiteDatabase sqLiteDatabase = mySQLiteHelper.getWritableDatabase();

            switch (flag) {

                case STUDENT:
                    long id = ContentUris.parseId(uri);

                    String whereselection = " id = " + id;

                    if (selection != null && !selection.equals("")) {
                        whereselection += " and " + selection;
                    }

                    cursor = sqLiteDatabase.query(MySQLiteHelper.TABLEC_NAME,
                            null, whereselection, selectionArgs, null, null, null);
                    break;

                case STUDENTS:
                    cursor = sqLiteDatabase.query(MySQLiteHelper.TABLEC_NAME,
                            null, selection, selectionArgs, null, null, null);
                    break;

            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        return cursor;
    }


    /*
    Implement this to handle requests for the MIME type of the data at the given URI
     */
    @Override
    public String getType(Uri uri) {

        int flag = URI_MATCHER.match(uri);
        switch (flag) {

            case STUDENT:
                //vnd.android.cursor.item for a single record,
                return "vnd.android.cursor.item/student";

            case STUDENTS:
                //vnd.android.cursor.dir/ for multiple items.
                return "vnd.android.cursor.dir/students";
        }
        return null;
    }

    @Override
    public Uri insert(Uri uri, ContentValues values) {

        Uri resultUri = null;

        int flag = URI_MATCHER.match(uri);

        switch (flag) {

            case STUDENTS:
                //数据库操作实例
                SQLiteDatabase sqLiteDatabase = mySQLiteHelper.getWritableDatabase();
                //数据库插入
                long id = sqLiteDatabase.insert(MySQLiteHelper.TABLEC_NAME, null, values);
                resultUri = ContentUris.withAppendedId(uri, id);
                break;

        }
        return resultUri;
    }

    @Override
    public int delete(Uri uri, String selection, String[] selectionArgs) {

        int count = -1;

        try {

            int flag = URI_MATCHER.match(uri);
            SQLiteDatabase sqLiteDatabase = mySQLiteHelper
                    .getWritableDatabase();

            switch (flag) {

                case STUDENT:

                    long id = ContentUris.parseId(uri);
                    String where_value = " id = " + id;
                    if (selection != null && !selection.equals("")) {
                        where_value += " and " + selection;
                    }
                    count = sqLiteDatabase.delete(MySQLiteHelper.TABLEC_NAME,
                            where_value, selectionArgs);

                    break;

                case STUDENTS:
                    count = sqLiteDatabase.delete(MySQLiteHelper.TABLEC_NAME,
                            selection, selectionArgs);
                    break;

            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        return count;
    }

    @Override
    public int update(Uri uri, ContentValues values, String selection,
                      String[] selectionArgs) {

        int count = -1;

        try {

            int flag = URI_MATCHER.match(uri);

            long id = ContentUris.parseId(uri);

            SQLiteDatabase sqLiteDatabase = mySQLiteHelper
                    .getWritableDatabase();

            switch (flag) {

                case STUDENT:

                    String whereClause = " id = " + id;

                    if (selection != null && !selection.equals("")) {
                        whereClause += " and " + selection;
                    }

                    count = sqLiteDatabase.update(MySQLiteHelper.TABLEC_NAME,
                            values, whereClause, selectionArgs);

                    break;

            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return count;
    }

}
